package com.epam.android.training.app.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * methods for reading/writing data from/to db
 * 
 * @author Natallia_Kukharchyk
 */
public class TestsDb {

  private final TestsOpenHelper helper;

  public TestsDb(Context context) {
    helper = new TestsOpenHelper(context);
  }

  public void insertTest(int testNum, int questionsNum, int answNum, String testTitle) {
    helper.insertTests(helper.getWritableDatabase(), testNum, questionsNum, answNum, testTitle);
    helper.close();
  }

  public boolean removeTest(Long test_id) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("test_id", test_id);
    return (db.delete(TestsOpenHelper.TESTS_TABLE_NAME, "test_id=" + test_id.toString(), null) > 0);
  }

  public Cursor getTestsCursor() {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(TestsOpenHelper.TESTS_TABLE_NAME, null, null, null, null, null,
        TestsOpenHelper.IS_NEW_FIELD + " DESC");
    return cursor;
  }

  public Cursor getOnlyNewTestsCursor() {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(TestsOpenHelper.TESTS_TABLE_NAME, null, TestsOpenHelper.IS_NEW_FIELD + "=?", new String[] {
      "1"
    }, null, null, TestsOpenHelper.TEST_ID_FIELD + " DESC");
    return cursor;
  }

  public Cursor getQuestionsForTestCursor(Long testId) {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(TestsOpenHelper.QUESTIONS_TABLE_NAME, null, TestsOpenHelper.TEST_ID_FIELD + "=?",
        new String[] {
          testId.toString()
        }, null, null, TestsOpenHelper.QUESTION_ID_FIELD);
    return cursor;
  }

  public Cursor getAnswersForQuestionCursor(Long question_id) {
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(TestsOpenHelper.ANSWERS_TABLE_NAME, null, TestsOpenHelper.QUESTION_ID_FIELD + "=?",
        new String[] {
          question_id.toString()
        }, null, null, TestsOpenHelper.ANSWER_ID_FIELD);
    return cursor;
  }

  public void writeScoreToDb(Long testId, int score) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TestsOpenHelper.LAST_SCORE_FIELD, score);
    values.put(TestsOpenHelper.IS_NEW_FIELD, 0);
    db.update(TestsOpenHelper.TESTS_TABLE_NAME, values, TestsOpenHelper.TEST_ID_FIELD + "=?", new String[] {
      testId.toString()
    });
  }

  public List<TestQuestion> getQuestionsForTest(Long test_id) {
    List<TestQuestion> list = new ArrayList<TestQuestion>();
    Cursor c = getQuestionsForTestCursor(test_id);
    c.moveToFirst();
    while (c.isAfterLast() == false) {
      TestQuestion question = new TestQuestion(c.getLong(0), c.getLong(1), c.getString(2), c.getInt(3));
      Cursor answersCursor = getAnswersForQuestionCursor(c.getLong(0));
      answersCursor.moveToFirst();
      List<String> answers = new ArrayList<String>();
      while (answersCursor.isAfterLast() == false) {
        answers.add(answersCursor.getString(2));
        answersCursor.moveToNext();
      }
      answersCursor.close();
      question.setAnswers(answers);
      list.add(question);
      c.moveToNext();
    }
    c.close();
    return list;
  }

  public List<Tests> getTests(boolean onlyNew) {
    List<Tests> list = new ArrayList<Tests>();
    Cursor c = (onlyNew) ? getOnlyNewTestsCursor() : getTestsCursor();
    c.moveToFirst();
    while (c.isAfterLast() == false) {
      Tests test = new Tests(c.getLong(0), c.getString(1), c.getString(2), c.getInt(3), c.getLong(4));
      list.add(test);
      c.moveToNext();
    }
    c.close();
    return list;
  }

  public void close() {
    helper.close();
  }

}